<?php
require_once(_PS_MODULE_DIR_.'prestatheme/controllers/admin/AdminThemeEditorController.php');
require_once(_PS_MODULE_DIR_.'prestatheme/controllers/admin/AdminConfigTabController.php');
require_once(_PS_MODULE_DIR_.'prestatheme/controllers/admin/AdminConfigItemController.php');
require_once(_PS_MODULE_DIR_.'prestatheme/controllers/admin/AdminConfigOptionController.php');

class AdminPrestathemeEditorController extends ModuleAdminController
{
	/** @var object adminThemeEditor() instance */
	protected $admin_theme_editor;

	/** @var object adminConfigTab() instance */
	protected $admin_config_tab;

	/** @var object adminConfigItem() instance */
	protected $admin_config_item;

	/** @var object adminConfigOption() instance */
	protected $admin_config_option;

	/** @var object ThemeEditor() instance for navigation*/
	protected static $editor = null;

	/** @var object ConfigTab() instance for navigation*/
	protected static $configTab = null;

	/** @var object ConfigItem() instance for navigation*/
	protected static $configParentItem = null;
    
	/** @var object ConfigItem() instance for navigation*/
	protected static $configItem = null;

	/** @var ConfigOption() instance for navigation*/
	protected static $configOption = null;

	public function __construct()
	{
        $this->table = 'pt_theme_editor';
		$this->className = 'ThemeEditor';
		$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?')));
        
		/* Get current editor */
		$id_editor  = (int)Tools::getValue('id_editor', 0);
        if (!empty($id_editor))
        {
            self::$editor = new ThemeEditor($id_editor);
    		if (!Validate::isLoadedObject(self::$editor))
    			die('Prestatheme Editor cannot be loaded');
        }
        
		$id_tab     = (int)Tools::getValue('id_tab', 0);
        if (!empty($id_tab))
        {
            self::$configTab = new ConfigTab($id_tab, Context::getContext()->language->id);
    		if (!Validate::isLoadedObject(self::$configTab))
    			die('Config tab cannot be loaded');
            self::$editor = new ThemeEditor(self::$configTab->id_editor);
        }
        
		$id_item    = (int)Tools::getValue('id_item', 0);
        if (!empty($id_item))
        {
            self::$configItem = new ConfigItem($id_item, Context::getContext()->language->id);
    		if (!Validate::isLoadedObject(self::$configItem))
    			die('Config Item cannot be loaded');
            self::$configParentItem = new ConfigItem(self::$configItem->id_parent, Context::getContext()->language->id);
            self::$configTab = new ConfigTab(self::$configItem->id_tab, Context::getContext()->language->id);
        }
        
		$id_option = (int)Tools::getValue('id_option', 0);
        if (!empty($id_option))
        {
            self::$configOption = new ConfigOption($id_option, Context::getContext()->language->id);
    		if (!Validate::isLoadedObject(self::$configOption))
    			die('Option cannot be loaded');
            self::$configItem = new ConfigItem(self::$configOption->id_item, Context::getContext()->language->id);
        }
        
        try
        {
    		$this->admin_theme_editor  = new AdminThemeEditorController();
    		$this->admin_config_tab    = new AdminConfigTabController();
    		$this->admin_config_item   = new AdminConfigItemController();
    		$this->admin_config_option = new AdminConfigOptionController();
        }
        catch (Exception $e)
        {
            die ($e);
        }
        
		parent::__construct();
	}

	/**
	 * Return current elements
	 *
	 * @return object
	 */
	public static function getCurrentEditor()
	{
		return self::$editor;
	}
	public static function getCurrentTab()
	{
		return self::$configTab;
	}
	public static function getCurrentParentItem()
	{
		return self::$configParentItem;
	}
	public static function getCurrentItem()
	{
		return self::$configItem;
	}
	public static function getCurrentOption()
	{
		return self::$configOption;
	}
    
    public function getOptionName ($echo, $row)
    {
        if (empty($row['label']))
            return '';
        elseif (empty($row['id_parent']))
            return $row['label'];
        $sql = '
            SELECT id_parent, label, id_option
            FROM `' . _DB_PREFIX_ . 'pt_config_option` o
            LEFT JOIN `'._DB_PREFIX_.'pt_config_option_lang` ol ON (o.`id_option` = ol.`id_opion` AND ol.`id_lang` = '.(int)$this->context->language->id.')
            WHERE `id_option` = '. $row['id_parent'];
        $new_row = Db :: getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        return $this->getOptionName($echo, $new_row) .' '. $row['label'];
    }

	public function viewAccess($disable = false)
	{
		$result = parent::viewAccess($disable);
		$this->admin_theme_editor->tabAccess  = $this->tabAccess;
		$this->admin_config_tab->tabAccess    = $this->tabAccess;
		$this->admin_config_item->tabAccess   = $this->tabAccess;
		$this->admin_config_option->tabAccess = $this->tabAccess;
		return $result;
	}
    
	public function initToolbar()
	{
		$this->toolbar_btn = array();
        $this->toolbar_scroll = false;
	}
    
	public function initContent()
	{
		$this->admin_theme_editor->token  = $this->token;
		$this->admin_config_tab->token    = $this->token;
		$this->admin_config_item->token   = $this->token;
		$this->admin_config_option->token = $this->token;
        
        $this->initToolbar();
        $this->content .= $this->renderView();
        $this->content .= $this->displayListHeader();
        
		if ($this->display == 'edit_editor' && !Tools::isSubmit('id_editor'))
        {
            $this->admin_theme_editor->display = 'edit';
            $this->admin_theme_editor->initContent();
			$this->content .= $this->admin_theme_editor->renderForm();
		}
        else if ($this->display == 'edit_tab' && !Tools::isSubmit('id_tab'))
		{
            $this->admin_config_tab->display = 'edit';
        	$this->admin_config_tab->initContent();
            $this->content .= $this->admin_config_tab->renderForm();
		}
        else if ($this->display == 'edit_item' && !Tools::isSubmit('id_item'))
		{
            $this->admin_config_item->display = 'edit';
        	$this->admin_config_item->initContent();
            $this->content .= $this->admin_config_item->renderForm();
		}
        else if ($this->display == 'edit_option' && !Tools::isSubmit('id_option'))
		{
            $this->admin_config_option->display = 'edit';
        	$this->admin_config_option->initContent();
            $this->content .= $this->admin_config_option->renderForm();
        }
		else
        {
    		$id_editor  = (int)Tools::getValue('id_editor', 0);
    		$id_tab     = (int)Tools::getValue('id_tab', 0);
    		$id_item    = (int)Tools::getValue('id_item', 0);
    		$id_option  = Tools::getValue('id_option', 0);
    
    		// breadcrumb
    		$cms_tabs = array('theme_editor', 'config_tab', 'config_item', 'config_option');
    		// Cleaning links
    		$cat_bar_index = self::$currentIndex;
    		foreach ($cms_tabs as $tab)
    			if (Tools::getValue($tab.'Orderby') && Tools::getValue($tab.'Orderway'))
    				$cat_bar_index = preg_replace('/&'.$tab.'Orderby=([a-z _]*)&'.$tab.'Orderway=([a-z]*)/i', '', self::$currentIndex);
                
            if ($id_option)
            {
                // View detail on current option
                $this->admin_config_option->display = 'edit';
                $this->admin_config_option->initContent();
                $this->content .= $this->admin_config_option->renderForm();
            }
            elseif ($id_item)
            {
                // View detail on current item
                $this->admin_config_item->display = 'edit';
                $this->admin_config_item->initContent();
                $this->content .= $this->admin_config_item->renderForm();
                
    			// Display the list of items which are sub items of the current item
    			$this->admin_config_item->setIdParentForFilter($id_item);
                $this->admin_config_item->display = 'list';
                $this->admin_config_item->initContent();
    			$this->content .= '<br />'. $this->admin_config_item->renderList();
                
                // Display the list of options of the current item
    			$this->admin_config_option->setIdItemForFilter($id_item);
                $this->admin_config_option->display = 'list';
                $this->admin_config_option->initContent();
    			$this->content .= '<br />'. $this->admin_config_option->renderList();
            }
            elseif ($id_tab)
            {
                // View detail on current tab
                $this->admin_config_tab->display = 'edit';
                $this->admin_config_tab->initContent();
                $this->content .= $this->admin_config_tab->renderForm();
                
                // Display the list of Main items for the current tab
    			$this->admin_config_item->setIdTabForFilter($id_tab);
                $this->admin_config_item->display = 'list';
                $this->admin_config_item->initContent();
    			$this->content .= '<br />'. $this->admin_config_item->renderList();
            }
            elseif ($id_editor)
            {
                // View detail on current editor
                $this->admin_theme_editor->display = 'edit';
                $this->admin_theme_editor->initContent();
                $this->content .= $this->admin_theme_editor->renderForm();
                
                // Display the list of config tabs for the current editor
                $this->admin_config_tab->id_editor = $id_editor;
                $this->admin_config_tab->display = 'list';
                $this->admin_config_tab->initContent();
    			$this->content .= '<br />'. $this->admin_config_tab->renderList();
            }
            else
            {
                // Display the list of editor
                $this->admin_theme_editor->display = 'list';
    			$this->admin_theme_editor->initContent();
    			$this->content .= $this->admin_theme_editor->renderList();
            }
            /*
    		$this->context->smarty->assign(array(
    			'config_tab_breadcrumb' => $this->_getPath($cat_bar_index, $id_editor),
    			'config_item_breadcrumb' => $this->_getPath($cat_bar_index, $id_editor, $id_tab),
    			'config_option_breadcrumb' => $this->_getPath($cat_bar_index, $id_editor, $id_tab, $id_item),
    		));
            */
        }
		$this->context->smarty->assign(array(
			'content' => $this->content,
			'base_url' => $this->module->getPathUri(),
		));
	}

	public function postProcess()
	{
		if (Tools::isSubmit('submitDelpt_config_option')
			|| Tools::isSubmit('submitAddpt_config_optionAndBackToParent')
			|| Tools::isSubmit('submitBulkdeletept_config_option')
			|| Tools::isSubmit('submitAddpt_config_option')
			|| Tools::isSubmit('deletept_config_option')
			|| Tools::isSubmit('viewpt_config_option')
			|| (Tools::isSubmit('statuspt_config_option') && Tools::isSubmit('id_option'))
			|| (Tools::isSubmit('position') && Tools::isSubmit('id_option_to_move')))
        {
            if (isset($_POST['id_item']) && !empty($_POST['name']) && (is_string($_POST['name']) && Validate::matchRegex('/^[a-zA-Z0-9_]+$/', $_POST['name']))) {
                $namespace = $this->admin_config_option->genNamespace($_POST['id_item']);
                $_POST['namespace'] = $namespace . $_POST['name'];
            } elseif (isset($_POST['id_item']) && isset($_POST['name'])) {
                $this->admin_config_option->errors[] = $this->l('Item is required and name is invalid');
            }
			$this->admin_config_option->initProcess();
			$this->admin_config_option->postProcess();
		}
        elseif (Tools::isSubmit('submitDelpt_config_item')
			|| Tools::isSubmit('previewSubmitAddpt_config_itemAndPreview')
			|| Tools::isSubmit('submitAddpt_config_item')
			|| Tools::isSubmit('deletept_config_item')
			|| Tools::isSubmit('viewpt_config_item')
			|| (Tools::isSubmit('statuspt_config_item') && Tools::isSubmit('id_item'))
			|| (Tools::isSubmit('way') && Tools::isSubmit('id_item')) && (Tools::isSubmit('position')))
        {
            if (isset($_POST['id_parent']) && empty($_POST['id_parent']))
                $_POST['id_parent'] = NULL;
            if (isset($_POST['id_tab']) && empty($_POST['id_tab']))
                $_POST['id_tab'] = NULL;
            if (empty($_POST['id_parent']) && empty($_POST['id_tab'])) {
                $this->admin_config_item->errors[] = $this->l('Tab and Parent item could not be "none" at the same time');
            } elseif (!empty($_POST['id_parent']) && !empty($_POST['id_tab'])) {
                $this->admin_config_item->errors[] = $this->l('Tab and Parent item could not be set at the same time. On of them should be "none"');
            }
            
			$this->admin_config_item->initProcess();
			$this->admin_config_item->postProcess();
		}
        elseif (Tools::isSubmit('submitDelpt_config_tab')
			|| Tools::isSubmit('previewSubmitAddpt_config_tabAndPreview')
			|| Tools::isSubmit('submitAddpt_config_tab')
			|| Tools::isSubmit('deletept_config_tab')
			|| Tools::isSubmit('viewpt_config_tab')
			|| (Tools::isSubmit('statuspt_config_tab') && Tools::isSubmit('id_tab'))
			|| (Tools::isSubmit('way') && Tools::isSubmit('id_tab')) && (Tools::isSubmit('position')))
		{
        	$this->admin_config_tab->initProcess();
        	$this->admin_config_tab->postProcess();
		}
        elseif (Tools::isSubmit('submitDelpt_theme_editor')
			|| Tools::isSubmit('previewSubmitAddpt_theme_editorAndPreview')
			|| Tools::isSubmit('submitAddpt_theme_editor')
			|| Tools::isSubmit('deletept_theme_editor')
			|| Tools::isSubmit('viewpt_theme_editor')
			|| (Tools::isSubmit('statuspt_theme_editor') && Tools::isSubmit('id_editor'))
			|| (Tools::isSubmit('way') && Tools::isSubmit('id_edito')) && (Tools::isSubmit('position')))
		{
        	$this->admin_theme_editor->initProcess();
        	$this->admin_theme_editor->postProcess();
        }
        
		if (((Tools::isSubmit('submitAddpt_config_option') || Tools::isSubmit('submitAddpt_config_optionAndStay')) && count($this->admin_theme_editor->errors))
			|| Tools::isSubmit('updatept_config_option')
			|| Tools::isSubmit('addpt_config_option'))
			$this->display = 'edit_option';
		else if (((Tools::isSubmit('submitAddpt_config_item') || Tools::isSubmit('submitAddpt_config_itemAndStay')) && count($this->admin_config_tab->errors))
			|| Tools::isSubmit('updatept_config_item')
			|| Tools::isSubmit('addpt_config_item'))
			$this->display = 'edit_item';
		else if (((Tools::isSubmit('submitAddpt_config_tab') || Tools::isSubmit('submitAddpt_config_tabAndStay')) && count($this->admin_config_tab->errors))
			|| Tools::isSubmit('updatept_config_tab')
			|| Tools::isSubmit('addpt_config_tab'))
			$this->display = 'edit_tab';
		else if (((Tools::isSubmit('submitAddpt_theme_editor') || Tools::isSubmit('submitAddpt_theme_editorAndStay')) && count($this->admin_config_tab->errors))
			|| Tools::isSubmit('updatept_theme_editor')
			|| Tools::isSubmit('addpt_theme_editor'))
			$this->display = 'edit_editor';
		else
		{
			$this->display = 'list';
			$this->id_editor = (int)Tools::getValue('id_editor');
		}

		if (isset($this->admin_config_option->errors))
			$this->errors = array_merge($this->errors, $this->admin_config_option->errors);

		if (isset($this->admin_config_item->errors))
			$this->errors = array_merge($this->errors, $this->admin_config_item->errors);

		if (isset($this->admin_config_tab->errors))
			$this->errors = array_merge($this->errors, $this->admin_config_tab->errors);

		if (isset($this->admin_theme_editor->errors))
			$this->errors = array_merge($this->errors, $this->admin_theme_editor->errors);

		parent::postProcess();
	}

	public function setMedia()
	{
		parent::setMedia();
		$this->addJqueryUi('ui.widget');
		/*$this->addJqueryUi('ui.draggable');
		$this->addJqueryUi('ui.droppable');*/
		$this->addJqueryPlugin('tagify');
		$this->addJqueryPlugin('autocomplete');
        $this->addCSS('/modules/prestatheme/views/css/admin-presta.css');
        $this->addCSS('/js/jquery/plugins/treeview-categories/jquery.treeview-categories.css');
	}
    
    protected function displayListHeader ($is_editor_filter = true) {
        $type = '';
        $id = 0;
        if (Tools::isSubmit('id_editor'))
        {
            $type = 'editor';
            $id   = (int)Tools::getValue('id_editor', 0);
        }
        elseif (Tools::isSubmit('id_tab'))
        {
            $type = 'tab';
            $id   = (int)Tools::getValue('id_tab', 0);
        }
        elseif (Tools::isSubmit('id_item'))
        {
            $type = 'item';
            $id   = (int)Tools::getValue('id_item', 0);
        }
        $editor_tree = $this->displayEditorTree ($this->getEditorTree(), array('type'=>$type,'id'=>$id));
        
        $base_url = 'index.php?controller=AdminPrestathemeEditor&token='. $this->token;
        $html = '
		<script type="text/javascript">
			$(document).ready(function(){
				var base_url = \''. $base_url .'\';
				// Load editor items page when editor is clicked
				$(\'#editor-treeview :input\').live(\'click\', function(){
					if (this.value !== "")
						location.href = base_url + \'&\' + $(this).attr("rel") + \'=\' + parseInt(this.value);
					else
						location.href = base_url;
				});

				// Make sure the checkbox is checked/unchecked when the link is clicked
				$(\'#toggle_editor_tree\').bind(\'click\', function(event){
					event.preventDefault();
					$(\'#block_editor_tree\').toggle();
					if ($(\'#block_editor_tree\').is(\':visible\'))
						$(this).find(\'input\').attr(\'checked\', true);
					else
					{
						$(this).find(\'input\').removeAttr(\'checked\');
						location.href = base_url;
					}
				});
			});

		</script>
		<div class="bloc-leadin">
			<div id="container_editor_tree">
				<div id="block_editor_tree">
        			<ul id="editor-treeview" class="filetree  treeview">
        				<li id="root" class="hasChildren last">
        					<span class="folder"><a href="index.php?controller=AdminPrestathemeEditor&token='. $this->token .'">'. $this->l('Editors') .'</a></span>
        					'. $editor_tree .'
        				</li>
        			</ul>
                    <div style="clear:both;"></div>
				</div>
			</div>
		</div>';
        
        return $html;
    }
    
    protected function getEditorTree () {
        // Get editor for each CMS
        $editors = Db::getInstance()->executeS('
            SELECT e.id_editor, CONCAT(c.code, \' \', c.version) as label 
            FROM `'. _DB_PREFIX_ .'pt_theme_editor` e 
            LEFT JOIN `'. _DB_PREFIX_ .'pt_cms` c ON c.id_cms = e.id_cms
            WHERE e.active = 1
            ORDER BY e.id_cms');
            
        // Get tabs for each editor
        if (is_array($editors))
            foreach ($editors as &$editor) {
                $editor['sub_elts'] = Db::getInstance()->executeS('
                SELECT t.id_tab, tl.label 
                FROM `'. _DB_PREFIX_ .'pt_config_tab` t 
                LEFT JOIN `'. _DB_PREFIX_ .'pt_config_tab_lang` tl ON (tl.id_tab = t.id_tab AND tl.id_lang = '. Context::getContext()->language->id .')
                WHERE t.active = 1 AND t.id_editor = '. (int)$editor['id_editor'] .'
                ORDER BY t.position');
            
                // Get items for each tabs
                if (is_array($editor['sub_elts']))
                    foreach ($editor['sub_elts'] as &$tab) {
                        $tab['sub_elts'] = Db::getInstance()->executeS('
                        SELECT i.id_item, il.label 
                        FROM `'. _DB_PREFIX_ .'pt_config_item` i 
                        LEFT JOIN `'. _DB_PREFIX_ .'pt_config_item_lang` il ON (il.id_item = i.id_item AND il.id_lang = '. Context::getContext()->language->id .')
                        WHERE i.active = 1 AND i.id_tab = '. (int)$tab['id_tab'] .'
                        ORDER BY i.position');
                        
                        // Get items recursively for each item parent
                        if (is_array($tab['sub_elts']))
                            foreach ($tab['sub_elts'] as &$item) {
                                $item['sub_elts'] = array();
                                $this->getRecursiveItems($item['id_item'], $item['sub_elts']);
                            }
                    }
            }
        return $editors;
    }
    
    protected function getRecursiveItems ($id_parent, &$tree) {
        if (!empty($id_parent)) {
            $tree = Db::getInstance()->executeS('
            SELECT i.id_item, il.label 
            FROM `'. _DB_PREFIX_ .'pt_config_item` i 
            LEFT JOIN `'. _DB_PREFIX_ .'pt_config_item_lang` il ON (il.id_item = i.id_item AND il.id_lang = '. Context::getContext()->language->id .')
            WHERE i.active = 1 AND i.id_parent= '. (int)$id_parent .'
            ORDER BY i.position');
            
            // Get items recursively for each item parent
            if (is_array($tree))
                foreach ($tree as &$item) {
                    $item['sub_elts'] = array();
                    $this->getRecursiveItems($item['id_item'], $item['sub_elts']);
                }
        }
    }
    
    protected function displayEditorTree ($tree, $selected = array()) {
        $html = '';
        $type = '';
        $typeLabel = '';
        $id = 0;
        
        if (is_array($tree))
        {
            $html .= '<ul>';
            $count = count($tree);
            foreach ($tree as $k=>$item) {
                $label = $item['label'];
                if (isset($item['id_editor']))
                {
                    $type = 'editor';
                    $typeLabel = $this->l('Editor');
                    $id   = (int)$item['id_editor'];
                }
                elseif (isset($item['id_tab']))
                {
                    $type = 'tab';
                    $typeLabel = $this->l('Tab');
                    $id   = (int)$item['id_tab'];
                }
                elseif (isset($item['id_item']))
                {
                    $type = 'item';
                    $typeLabel = $this->l('Item');
                    $id   = (int)$item['id_item'];
                }
                
                $html .= '
                <li class="'. (!empty($item['sub_elts']) ? 'hasChildren' : '') .''. (($k == ($count-1)) ? ' last' : '') .'">
                    <input type="radio" name="current_editor_elt" rel="id_'. $type .'" id="'. $type .'_'. $id .'" value="'. $id .'"'. ((isset($selected['type']) && isset($selected['id']) && $selected['type'] == $type && $selected['id'] == $id) ? ' checked="checked"' : '') .' /> 
                    <label for="'. $type .'_'. $id .'">'. $label .' ('. $typeLabel .')</label>
                    '. (!empty($item['sub_elts']) ? $this->displayEditorTree($item['sub_elts'], $selected) : '')  .'
                </li>
                ';
            }
            $html .= '</ul>';
		}
        return $html;
    }

	public function ajaxProcessUpdateConfigOptionPositions()
	{
		if ($this->tabAccess['edit'] === '1')
		{
			$id_option = (int)Tools::getValue('id_option');
			$id_item   = (int)Tools::getValue('id_item');
			$id_tab    = (int)Tools::getValue('id_tab');
			$id_editor = (int)Tools::getValue('id_editor');
			$way = (int)Tools::getValue('way');
			$positions = Tools::getValue('pt_config_option');
			if (is_array($positions))
				foreach ($positions as $key => $value)
				{
					$pos = explode('_', $value);
					if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_item && $pos[2] == $id_option))
					{
						$position = $key;
						break;
					}
				}
			$option = new ConfigOption($id_option);
			if (Validate::isLoadedObject($option))
			{
				if (isset($position) && $option->updatePosition($way, $position))
					die(true);
				else
					die('{"hasError" : true, "errors" : "Can not update option position"}');
			}
			else
				die('{"hasError" : true, "errors" : "This option can not be loaded"}');
		}
	}

	public function ajaxProcessUpdateConfigItemPositions()
	{
		if ($this->tabAccess['edit'] === '1')
		{
			$id_item   = (int)Tools::getValue('id_item');
			$id_tab    = (int)Tools::getValue('id_tab');
			$id_editor = (int)Tools::getValue('id_editor');
			$way = (int)Tools::getValue('way');
			$positions = Tools::getValue('pt_config_item');
			if (is_array($positions))
				foreach ($positions as $key => $value)
				{
					$pos = explode('_', $value);
					if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_tab && $pos[2] == $id_item))
					{
						$position = $key;
						break;
					}
				}
			$item = new ConfigItem($id_item);
			if (Validate::isLoadedObject($item))
			{
				if (isset($position) && $item->updatePosition($way, $position))
					die(true);
				else
					die('{"hasError" : true, "errors" : "Can not update main item position"}');
			}
			else
				die('{"hasError" : true, "errors" : "This main item can not be loaded"}');
		}
	}

	public function ajaxProcessUpdateConfigTabPositions()
	{
		if ($this->tabAccess['edit'] === '1')
		{
			$id_tab    = (int)Tools::getValue('id_tab');
			$id_editor = (int)Tools::getValue('id_editor');
			$way = (int)Tools::getValue('way');
			$positions = Tools::getValue('pt_config_tab');
			if (is_array($positions))
				foreach ($positions as $key => $value)
				{
					$pos = explode('_', $value);
					if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_editor && $pos[2] == $id_tab))
					{
						$position = $key;
						break;
					}
				}
			$tab = new ConfigOption($id_tab);
			if (Validate::isLoadedObject($tab))
			{
				if (isset($position) && $tab->updatePosition($way, $position))
					die(true);
				else
					die('{"hasError" : true, "errors" : "Can not update option position"}');
			}
			else
				die('{"hasError" : true, "errors" : "This option can not be loaded"}');
		}
	}
    
    protected function _getPath ($urlBase, $id_editor = NULL, $id_tab = NULL, $id_item = NULL)
    {
        $editor = $configTab = $configItem = $configOption = NULL;
        if (!empty($id_editor))
        {
            $editor = new ThemeEditor($id_editor);
            if (!empty($id_tab))
            {
                $configTab = new ConfigTab($id_tab, Context::getContext()->language->id);
                if (!empty($id_item))
                {
                    $configItem = new ConfigItem($id_item, Context::getContext()->language->id);
                    if (!empty($id_option))
                    {
                        $configOption = new ConfigOption($id_option, Context::getContext()->language->id);
                    }
                }
            }
        }
        
        if (empty($editor->id))
            return '';
        
        $path = '';
        if (!empty($editor->id))
        {
            $name = '<span class="highlight">'. $editor->getCMSName() .'</span>';
            $edit = '<a href="'.$urlBase.'&id_editor='.$editor->id.'&addpt_theme_editor&token=' . Tools::getAdminTokenLite('AdminPrestathemeEditor').'">
                <img src="../img/admin/edit.gif" alt="Modify" /></a> ';
            $path .= $edit.'<a href="'.$urlBase.'&id_editor='.$editor->id.'&viewpt_theme_editor&token=' . Tools::getAdminTokenLite('AdminPrestathemeEditor').'">
                '.$name.'</a>';
        }
        if (!empty($configTab->id))
        {
            $name = '<span class="highlight">'. $configTab->label .'</span>';
            $edit = ' &lt; <a href="'.$urlBase.'&id_editor='.$editor->id.'&id_tab='.$configTab->id.'&addpt_config_tab&token=' . Tools::getAdminTokenLite('AdminPrestathemeEditor').'">
                <img src="../img/admin/edit.gif" alt="Modify" /></a> ';
            $path .= $edit.'<a href="'.$urlBase.'&id_editor='.$editor->id.'&id_tab='.$configTab->id.'&viewpt_config_tab&token=' . Tools::getAdminTokenLite('AdminPrestathemeEditor').'">
                '.$name.'</a>';
        }
        if (!empty($configItem->id))
        {
            $name = '<span class="highlight">'. $configItem->label .'</span>';
            $edit = ' &lt; <a href="'.$urlBase.'&id_editor='.$editor->id.'&id_tab='.$configTab->id.'&id_item='.$configTab->id.'&addpt_config_item&token=' . Tools::getAdminTokenLite('AdminPrestathemeEditor').'">
                <img src="../img/admin/edit.gif" alt="Modify" /></a> ';
            $path .= $edit.'<a href="'.$urlBase.'&id_editor='.$editor->id.'&id_tab='.$configTab->id.'&id_item='.$configTab->id.'&viewpt_config_item&token=' . Tools::getAdminTokenLite('AdminPrestathemeEditor').'">
                '.$name.'</a>';
        }
        if (!empty($configOption->id))
        {
            $name = '<span class="highlight">'. $configOption->label .'</span>';
            $path .= ' &lt; '.$name.'</a>';
        }
        
        return ;
    }

}
